/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UI.Tree;

import Common.IProject;
import Common.IProjectModel;
import UI.AddDialog;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.MutableTreeNode;

/**
 *
 * @author Администратор
 */
public class RootNode extends DefaultMutableTreeNode implements ITree, IContextCommand {

    private IProjectModel model;
    private JTree tree;
    private JPopupMenu popupMenu;
    
    public RootNode(Object userObject, IProjectModel model, JTree tree) {
        super(userObject, true);
        //--------------------------------------------------
        this.popupMenu = new JPopupMenu();
        this.popupMenu.add(new ContextCommandItem(this, "Create Project"));
        //-------------------------------------------------
        this.model = model;
        this.tree = tree;
        for (IProject project : this.model.getProjects()) {
            this.add(new ProjectNode(project, this));
        }
        //OverloadPopupMenu();
    }

    // <editor-fold defaultstate="collapsed" desc="ITree Initialize">
    public JTree getTree() {
        return this.tree;
    }

    public JPopupMenu getPopupMenu() {
        return this.popupMenu;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="IContextCommand Initialize">
    public void DoCommand(ContextCommandItem item) {
        AddDialog adp = new AddDialog(null, true, "Добавить", "Ввеидет название проэкта");
        adp.setVisible(true);
        if (AddDialog.DISPOSE_ON_CLOSE != adp.getDefaultCloseOperation()) {
            IProject project = this.model.BuildProject();
            this.model.getProjects().add(project);
            project.setName(adp.getValue());
            this.add(new ProjectNode(project, this));
            this.getTree().updateUI();
        }
    }
    // </editor-fold>

    @Override
    public void remove(MutableTreeNode aChild) {
        this.model.getProjects().remove(((ProjectNode)aChild).getProject());
        super.remove((DefaultMutableTreeNode) aChild);
        this.getTree().updateUI();
    }

    
}
